package models

import (
	"leetcode-problem-set/gorm/pkg"
	"os"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Init() {
	dsn := "root:admin123@tcp(127.0.0.1:3306)/gorm_test?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		pkg.LogOutput(os.Stdout, "[连接数据库]", err)
		return
	}
	err = db.AutoMigrate(&User{}, &Article{}, &Comments{})
	if err != nil {
		pkg.LogOutput(os.Stdout, "[数据迁移]", err)
		return
	}
	DB = db
}
